<?php
class item_temporaryban extends template_item
{
	var $default_name = "Temporary Ban a Member";
	var $default_description = "Use this item to ban a member for a limited time.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['protected'] = array($this->ipsclass->vars['admin_group']);
			$this->item_config['max_day'] = 7;
			$this->item_config['subject'] = 'You have been banned!';
			$this->item_config['message'] = 
<<<EOF
{member_name},

You have been banned from {board_name} starting from {start_time} to {end_time} by {user_name}. You can either wait out the time or have someone use the unban item.
EOF;
		}
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$groups[] = array($g_id,$group['g_title']);
		}
		
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$value = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->item_config['message'] ) );
			$value = $this->ipsclass->parser->convert_ipb_html_to_html( $value );
		}
		else
		{
			$value = $this->ipsclass->parser->pre_edit_parse( $this->item_config['message'] );
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this item?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][protected][]", $groups, $this->item_config['protected']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What is the maximum number of days can someone ban someone else for?</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][max_day]", $this->item_config['max_day'])." days",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Email Subject:</b>",
															$this->ipsclass->adskin->form_input('subject',$this->item_config['subject']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Email Message:</b><br />Tags:<br />{board_name} = Name of the Board<br />{member_name} = Member Name of one being banned<br />{user_name} = Member Name of one using the item<br />{start_time} = Start time of the ban<br />{end_time} = End time of the ban<br />{length} = Length of the ban",
															$this->ipsclass->han_editor->show_editor( $value, 'message' ),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['protected'] = $input['protected'] ? $input['protected'] : array();
		$save['max_day'] = intval($input['max_day']);
		$save['subject'] = $this->ipsclass->input['subject'];
        $save['message'] = $this->ipsclass->han_editor->process_raw_post( 'message' );
		$save['message'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $save['message'] ) );
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['temp_ban_an_user']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['how_long']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='time' value='' size='5' class='forminput' />
				&nbsp;
				<select name='units'>
					<option value='h'>{$this->ipsclass->lang['hours']}</option>
					<option value='d'>{$this->ipsclass->lang['days']}</option>
				</select>
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['temp_ban_this_user']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['time']) && isset($this->ipsclass->input['units']) && isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_user'));
		}
		if($member['temp_ban'])
		{
			$this->ipsclass->Error(array('MSG'=>'been_banned'));
		}
		
		if(in_array($member['mgroup'],$this->item_config['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'protected_user'));
		}
		
		$this->ipsclass->input['time'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['time'],'int');
		
		$seconds = $this->ipsclass->input['units'] == 'd' ? 86400 : 3600;
		$duration = $seconds * $this->ipsclass->input['time'];
		if($this->ipsclass->input['time'] <= 0 or $duration > $this->item_config['max_day'] * 86400)
		{
			$this->ipsclass->Error(array('MSG'=>'banned_days_wrong'));
		}
		
		$ban_line = $this->ipsclass->hdl_ban_line(array('timespan' => $this->ipsclass->input['time'],'unit' => $this->ipsclass->input['units']));
		
		$this->ipsclass->DB->do_update('members',array(
															'temp_ban' => $ban_line
									),"id=".$member['id']);
		
		$this->ipsclass->lang['other_member_banned'] = str_replace("<#NAME#>",$member['members_display_name'],$this->ipsclass->lang['other_member_banned']);
		
		$subject = $this->item_config['build_multipart'];
		$message = str_replace("{board_name}",$this->ipsclass->vars['board_name'],$this->item_config['message']);
		$message = str_replace("{member_name}",$member['members_display_name'],$this->item_config['message']);
		$message = str_replace("{user_name}",$this->ipsclass->member['members_display_name'],$message);
		$message = str_replace("{start_time}",$this->ipsclass->get_date(time(),'LONG',1),$message);
		$message = str_replace("{end_time}",$this->ipsclass->get_date(time() + $duration,'LONG',1),$message);
		$message = str_replace("{length}",$this->ipsclass->numbers->time_string($duration,'LONG'),$message);
		require_once( ROOT_PATH."sources/classes/class_email.php" );
		
		$email           =  new emailer();
		$email->ipsclass =& $this->ipsclass;
		$email->email_init();
		
		$email->subject = $subject;
		$email->message = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $message ) );
		$email->to      = $member['email'];

		$email->send_mail();
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Banned <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a> for {$this->ipsclass->input['time']} ".($this->ipsclass->input['units'] == 'd' ? "days" : "hours"));
		
		$this->ipsclass->market_class->redirect('other_member_banned');
	}
}
?>